#include "customitemeditor.h"
#include "storenums.h"

#include <QLineEdit>
#include <QCheckBox>
#include <QComboBox>
#include <QMetaObject>
#include <QMetaEnum>

#include "defines.h"
#include <QDebug>
CustomItemEditor::CustomItemEditor(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget * CustomItemEditor::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
{
    QWidget *res = 0;

    //qDebug()<< index.model()->headerData(index.column(),Qt::Horizontal);
    QString columnName = index.model()->headerData(index.column(),Qt::Horizontal).toString();
    if(index.model()->objectName().compare(XML_COLUMN) == 0)
    {
        if(columnName.compare(XML_COLUMNTYPE)==0)
        {
            res = new QComboBox(parent);
        }
        else if(columnName.compare(XML_ISPRIMARYKEY)==0)
        {
            res = new QComboBox(parent);//QCheckBox("TEST",parent);
        }
    }
    if( res == 0 )
        res = new QLineEdit(parent);

    return res;
}

void CustomItemEditor::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if(index.model()->objectName().compare(XML_COLUMN) == 0)
    {
        QString columnName = index.model()->headerData(index.column(),Qt::Horizontal).toString();
        if(columnName.compare(XML_COLUMNTYPE)==0)
        {
            QComboBox *combo = qobject_cast<QComboBox*>(editor);
            if(combo)
            {
                populateComboFromEnum("DbFieldType",combo);
                QString type = index.model()->data(index, Qt::EditRole).toString();

                for(int i = 0;i<combo->count();i++)
                {
                    if(combo->itemText(i).compare(type)==0)
                    {
                        combo->setCurrentIndex(i);
                        break;
                    }
                }
            }
            return;
        }
        else if(columnName.compare(XML_ISPRIMARYKEY)==0)
        {
            QComboBox *combo = qobject_cast<QComboBox*>(editor);
            if(combo)
            {
                populateComboFromEnum("TrueFalse",combo);
                bool val = index.model()->data(index, Qt::EditRole).toBool();
                combo->setCurrentIndex(val);
            }
//            QCheckBox *checkbox = qobject_cast<QCheckBox*>(editor);
//            if(checkbox)
//            {
//                bool b = index.model()->data(index, Qt::EditRole).toBool();
//                checkbox->setChecked(b);
//            }
            return;
        }
    }

    QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
    if(lineEdit)
        lineEdit->setText( index.model()->data(index, Qt::EditRole).toString() );
    else
        qDebug()<<"CustomItemEditor. Unknown editor object: "<<editor;
}

void CustomItemEditor::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if(model->objectName().compare(XML_COLUMN)==0)
    {
        QString columnName = model->headerData(index.column(),Qt::Horizontal).toString();
        if(columnName.compare(XML_COLUMNTYPE)==0)
        {
            QComboBox *combo = qobject_cast<QComboBox*>(editor);
            model->setData(index, combo->currentText(), Qt::EditRole);
            model->setData(index, combo->currentText(), Qt::DisplayRole);            
            return;
        }
        else if( columnName.compare(XML_ISPRIMARYKEY)==0 )
        {
            QComboBox *combo = qobject_cast<QComboBox*>(editor);
            model->setData(index, combo->currentIndex(), Qt::EditRole);
            model->setData(index, combo->currentText(), Qt::DisplayRole);
            return;
        }
    }

    QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
    if(lineEdit)
        model->setData(index, lineEdit->text(), Qt::EditRole);
    else
        qDebug()<<"CustomItemEditor setModelData. Unknown editor object: "<<editor;
}

void CustomItemEditor::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect);
}

void CustomItemEditor::populateComboFromEnum(const QString &enumName, QComboBox *combo) const
{
    const QMetaObject* meta = StorEnums().metaObject();
    int i = meta->indexOfEnumerator(enumName.toLatin1());
    QMetaEnum _enum = meta->enumerator(i);
    for( int j = 0; j < _enum.keyCount(); j++)
    {
        combo->insertItem(j,_enum.key(j));
    }
}
